#encoding:utf-8
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_bcrypt import Bcrypt
from config import config
from flask_moment import Moment

#实例化扩展
mail = Mail()
db = SQLAlchemy()
bcrypt = Bcrypt()
moment = Moment()
login_manager = LoginManager()
login_manager.session_protection = "strong"
login_manager.login_view = "auth.login"

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    #初始化扩展
    mail.init_app(app)
    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)
    moment.init_app(app)

    # 导入过滤器
    from .filters import email_login_websit, format_user_status
    env = app.jinja_env
    env.filters["email_login_websit"] = email_login_websit
    env.filters["format_user_status"] = format_user_status

    # 导入蓝本
    from .main import main as main_blueprint
    from .auth import auth as auth_blueprint
    from .article import article as article_blueprint
    from .category import category as category_blueprint
    from .user import user as user_blueprint

    from .models import User, Role, Article, Category
    # 注册蓝本
    app.register_blueprint(main_blueprint)
    app.register_blueprint(auth_blueprint, url_prefix="/auth")
    app.register_blueprint(article_blueprint, url_prefix="/article")
    app.register_blueprint(category_blueprint, url_prefix="/category")
    app.register_blueprint(user_blueprint, url_prefix="/user")

    return app



